package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountLogMapper;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Slf4j
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;
    @Autowired
    private AccountLogMapper accountLogMapper;

    @Override
    public void decrIntergral(OperateIntergralVo operateIntergralVo) {
        //往t_account表中插入数据
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setGmtTime(new Date());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setType(AccountLog.TYPE_DECR);
        accountLogMapper.insert(accountLog);
        //减少用户积分
        int count =usableIntegralMapper.decrIntergral(operateIntergralVo.getUserId(),operateIntergralVo.getValue());
        if(count == 0){
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    public void incrIntergral(OperateIntergralVo operateIntergralVo) {
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setGmtTime(new Date());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setType(AccountLog.TYPE_INCR);
        accountLogMapper.insert(accountLog);
        usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(),operateIntergralVo.getValue());
    }

    @Override
    public void decrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        common(operateIntergralVo,context,AccountTransaction.STATE_CANCEL,AccountTransaction.INCREASE_POINTS);
    }

    @Override
    public void decrIntergralCommit(BusinessActionContext context) {
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null){
           log.info("异常处理");
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())){
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext().get("operateIntergralVo").toString(),OperateIntergralVo.class);
            int count = accountTransactionMapper.updateAccountTransactionState(accountTransaction.getTxId(),accountTransaction.getActionId(),AccountTransaction.STATE_COMMIT,AccountTransaction.STATE_TRY);
            if(count==0){
                log.info("异常处理");
            }else if(accountTransaction.getState() == AccountTransaction.STATE_COMMIT){

            }else{
                log.info("积分支付有问题");
            }
        }
    }

    @Override
    public void decrIntergralRollback(BusinessActionContext context) {
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext().get("operateIntergralVo").toString(),OperateIntergralVo.class);
        if (accountTransaction == null){
            common(operateIntergralVo,context,AccountTransaction.STATE_CANCEL,AccountTransaction.INCREASE_POINTS);
            return;
       }
    }

    @Override
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
       log.info("进入到try方法中");
      common(operateIntergralVo,context,AccountTransaction.STATE_TRY,AccountTransaction.INCREASE_POINTS);
    }

    @Override
    public void incrIntergralCommit(BusinessActionContext context) {
        log.info("进入到coonfirm的业务逻辑中");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null){
            log.info("异常处理");
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())){
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext().get("operateIntergralVo").toString(),OperateIntergralVo.class);
            usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(),operateIntergralVo.getValue());
            int count = accountTransactionMapper.updateAccountTransactionState(accountTransaction.getTxId(),accountTransaction.getActionId(),AccountTransaction.STATE_COMMIT,AccountTransaction.STATE_TRY);
            if(count==0){
                log.info("异常处理");
            }else if(accountTransaction.getState() == AccountTransaction.STATE_TRY){
                   //处理幂属性
            }else{
                log.info("积分支付有问题");
            }
        }
    }


    public void common(OperateIntergralVo operateIntergralVo, BusinessActionContext context,Integer state ,Integer type){
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setActionId(context.getBranchId() +"");
        accountTransaction.setTxId(context.getXid());
        accountTransaction.setAmount(context.getBranchId());
        accountTransaction.setGmtCreated(new Date());
        accountTransaction.setGmtModified(new Date());
        accountTransaction.setUserId(operateIntergralVo.getUserId());
        accountTransaction.setState(state);
        accountTransaction.setType(type+"");
        accountTransactionMapper.insert(accountTransaction);
    }

    @Override
    public void incrIntergralRollback(BusinessActionContext context) {
        log.info("进入到cancel的业务的逻辑中");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext().get("operateIntergralVo").toString(),OperateIntergralVo.class);
        if (accountTransaction == null){
             //插入一条记录
            common(operateIntergralVo,context,AccountTransaction.STATE_CANCEL,AccountTransaction.INCREASE_POINTS);
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())){
            //修改状态为取消状态
            accountTransactionMapper.updateAccountTransactionState(accountTransaction.getTxId(),accountTransaction.getActionId(),AccountTransaction.STATE_CANCEL,AccountTransaction.STATE_TRY);
        }else if (AccountTransaction.STATE_CANCEL.equals(accountTransaction.getState())){
            //处理幂等性
        }else {
            log.info("积分退款有问题");
        }
    }

}
